package com.example.demo.utils;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//



import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TinyUUIDGenerate {
    private static final Logger LOGGER = LoggerFactory.getLogger(TinyUUIDGenerate.class);
    private static final String LOCAL_LOOP_BACK_ADDRESS = "70000001";
    private static final String COUNT_INDEX_PATTERN = "000000";
    private static final String SHORT_COUNT_INDEX_PATTERN = "00000";
    private static final int MAX_COUNT_INDEX = 990000;
    private static final int MAX_SHORT_COUNT_INDEX = 90000;
    private static AtomicInteger COUNT = new AtomicInteger(0);
    private static AtomicInteger SHORT_COUNT = new AtomicInteger(0);
    private static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    private static String JVM_TAG;
    private static String SHORT_JVM_TAG;

    public TinyUUIDGenerate() {
    }

    public static String getCurrentTimeStampStr() {
        return getCurrentTimeStampStr(LocalDateTime.now());
    }

    public static String getCurrentTimeStampStr(LocalDateTime dateTime) {
        return DATETIME_FORMATTER.format(dateTime);
    }

    private static String getCount() {
        int count = COUNT.incrementAndGet();
        if (count > 990000) {
            synchronized(JVM_TAG.intern()) {
                if (COUNT.get() > 990000) {
                    COUNT.set(0);
                }
            }

            return String.valueOf(COUNT.incrementAndGet());
        } else {
            return String.valueOf(count);
        }
    }

    private static String getShortCount() {
        int count = SHORT_COUNT.incrementAndGet();
        if (count > 90000) {
            synchronized(SHORT_JVM_TAG.intern()) {
                if (SHORT_COUNT.get() > 90000) {
                    SHORT_COUNT.set(0);
                }
            }

            return String.valueOf(SHORT_COUNT.incrementAndGet());
        } else {
            return String.valueOf(count);
        }
    }

    public static String generate() {
        return generate(LocalDateTime.now());
    }

    public static String generate(LocalDateTime dateTime) {
        if (dateTime == null) {
            dateTime = LocalDateTime.now();
        }

        StringBuilder builder = new StringBuilder(getCurrentTimeStampStr(dateTime));
        String counter = getCount();
        builder.append("000000".substring(counter.length()));
        builder.append(counter);
        builder.append(JVM_TAG);
        builder.append(ThreadLocalRandom.current().nextInt(10));
        return builder.toString();
    }

    public static String generateShort() {
        StringBuilder builder = new StringBuilder(getCurrentTimeStampStr(LocalDateTime.now()));
        String counter = getShortCount();
        builder.append("00000".substring(counter.length()));
        builder.append(counter);
        builder.append(SHORT_JVM_TAG);
        return builder.toString();
    }

    private static String[] convertIpAddressBytes(byte[] bytes) {
        String[] result = new String[bytes.length];

        for(int i = 0; i < bytes.length; ++i) {
            result[i] = String.valueOf(bytes[i] & 255);
        }

        return result;
    }

    public static String formatIp(String[] ipaddress) {
        StringBuilder builder = new StringBuilder();
        builder.append(ipaddress[0].substring(ipaddress[0].length() - 1));
        builder.append(ipaddress[1].substring(ipaddress[1].length() - 1));
        builder.append((new StringBuilder("000")).replace(3 - ipaddress[2].length(), 3, ipaddress[2]));
        builder.append((new StringBuilder("000")).replace(3 - ipaddress[3].length(), 3, ipaddress[3]));
        return builder.toString();
    }

    public static void main(String[] args) {
        try {
            System.out.println("IP:" + ArrayUtils.toString(InetAddress.getLocalHost().getAddress()));
            System.out.println("IP:" + InetAddress.getLocalHost().getHostAddress());
            System.out.println(JVM_TAG);
            long time = System.currentTimeMillis();

            int i;
            for(i = 1; i < 1000000; ++i) {
                generate();
            }

            System.out.println(System.currentTimeMillis() - time);
            System.out.println(generate());
            System.out.println(generate());
            time = System.currentTimeMillis();

            for(i = 1; i < 1000000; ++i) {
                generateShort();
            }

            System.out.println(System.currentTimeMillis() - time);
            System.out.println(generateShort());
            System.out.println(generateShort());
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }

    static {
        try {
            JVM_TAG = formatIp(convertIpAddressBytes(InetAddress.getLocalHost().getAddress()));
            if ("70000001".equals(JVM_TAG)) {
                LOGGER.warn("tiny-boot-common UUID工具类获取IP地址 匹配到了本机回环地址127.0.0.1 启动IP地址随机生成策略 请检查当前实例HOSTS配置... hostName:{}", InetAddress.getLocalHost().getHostName());
                throw new IllegalAccessException("UUID工具类获取实例IP地址失败 127.0.0.1");
            }
        } catch (Exception var7) {
            String[] randomAddress = new String[4];
            ThreadLocalRandom random = ThreadLocalRandom.current();

            for(int i = 0; i < randomAddress.length; ++i) {
                randomAddress[i] = String.valueOf(random.nextInt(100) + random.nextInt(2, 8) * 100);
            }

            JVM_TAG = formatIp(randomAddress);
            LOGGER.error("主键生成工具初始化本地IP异常 请检查.. JVM_TAG:" + JVM_TAG, var7);
        } finally {
            SHORT_JVM_TAG = JVM_TAG.substring(2);
            generate();
            generateShort();
        }

    }
}

